package me.wangxuan.pat.view;

import java.awt.Component;
import java.awt.event.ActionListener;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

public class ButtonEditor extends AbstractCellEditor implements TableCellEditor {
	private static final long serialVersionUID = 1L;
	private final JButton button;
    private String text;

    public ButtonEditor(ActionListener actionListener) {
        button = new JButton();
        button.addActionListener(actionListener);
        button.setFocusPainted(false);
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
                                              boolean isSelected, int row, int column) {
        if (value instanceof String) {
            text = (String) value;
            button.setText(text.trim());
            button.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
            button.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
        }
//        button.setPreferredSize(new Dimension(23, 23)); // 设置按钮大小
        button.putClientProperty("row", row);
        return button;
    }

    @Override
    public Object getCellEditorValue() {
        return text;
    }
}
